//
//  TJUseActivationCodeController.m
//  BaoDian
//
//  Created by mac on 2024/7/30.
//

#import "TJUseActivationCodeController.h"
#import "TJButton.h"
#import "QMUITextField.h"


@interface TJUseActivationCodeController ()<QMUITextFieldDelegate>

@property (nonatomic, weak) UIView *contentView;
@property (nonatomic, weak) UIView *maskView;
@property (nonatomic, weak) TJButton *confirmButton;

@end

@implementation TJUseActivationCodeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = UIColor.clearColor;
    
    UIButton *maskView = [[UIButton alloc] initWithFrame:self.view.bounds].addTo(self.view);
    maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    [maskView addTarget:self action:@selector(didClickDismissButton) forControlEvents:UIControlEventTouchUpInside];
    _maskView = maskView;
    
    UIView *contentView = [UIView addToView:maskView];
    contentView.backgroundColor = UIColorMake(0x1E1F23);
    _contentView = contentView;
    
    UIView *titleView = [UIView addToView:contentView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.right.offset(0);
        make.height.offset(110);
    }];
    
    UILabel *titleLabel = [UILabel lableWithSuperView:titleView font:UIFontMake(13) color:UIColor.whiteColor title:@"是否确认使用码段为:" textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.offset(16);
    }];
    
    UILabel *codeLabel = [UILabel lableWithSuperView:titleView font:UIFontSemiboldMake(24) color:UIColorMake(0x2978F0) title:_code textAlignment:NSTextAlignmentLeft];
    [codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(titleLabel.mas_bottom).offset(3);
    }];
    
    UILabel *subTextLabel = [UILabel lableWithSuperView:titleView font:UIFontMake(13) color:UIColor.whiteColor title:@"的激活码使用后不可恢复" textAlignment:NSTextAlignmentLeft];
    [subTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(codeLabel.mas_bottom).offset(8);
    }];
 
    TJButton *cancelButton = [TJButton buttonWithSuperView:contentView font:UIFontMake(16) color:UIColor.whiteColor title:@"取消"];
    [cancelButton addTarget:self action:@selector(didClickDismissButton) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.inset(TabbarSafeBottomMargin);
        make.height.offset(54);
        make.left.right.inset(0);
    }];
    
    UIView *marginView = [UIView addToView:contentView];
    marginView.backgroundColor = UIColorMake(0x2A2B31);
    [marginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(0);
        make.height.offset(8);
        make.bottom.equalTo(cancelButton.mas_top);
    }];
    
    TJButton *confirmButton = [TJButton buttonWithSuperView:contentView font:UIFontMake(16) color:UIColorMake(0x2978F0) title:@"使用"];
    [confirmButton addTarget:self action:@selector(didClickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(cancelButton);
        make.bottom.equalTo(marginView.mas_top);
    }];
    
    UIView *lineView = [UIView addToView:contentView];
    lineView.backgroundColor = UIColorMake(0x2A2B31);
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(0);
        make.height.offset(1);
        make.bottom.equalTo(confirmButton.mas_top);
    }];
    
    CGFloat contentViewH = TabbarSafeBottomMargin + 54 * 2 + 8 + 110;
    contentView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, contentViewH);
    [contentView addRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight radius:20];
}

- (void)textFieldDidChange:(UITextField *)textField {
    _confirmButton.enabled = !isEmptyString(textField.text);
}
 
- (void)didClickConfirmButton {
    
    dispatch_async_sql_queue(^{
        TJUserModel *userModel = [TJUserModel currentAccountModel];
        dispatch_async_main_queue(^{
            [TJHUD showLoadingInView:self.view];
            [TJMinePresent createStaffGroupWithActiveCode:self.code name:[NSString stringWithFormat:@"新分组-%@", [[NSDate date] stringWithFormat:@"yyyy-MM-dd HH:mm:ss"]] supplierId:userModel.Id complete:^(NSError * _Nullable error) {
                if (error) {
                    return;
                }
                
                [self dismissComplete:^{
                    if (self.complete) {
                        self.complete();
                    }
                }];
            }];
        });
    });
    

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

 
- (void)didClickDismissButton {
    [self dismissComplete:nil];
}

- (void)didClickMaskButton {
    [self.view endEditing:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.tj_y = KScreenHeight - self.contentView.tj_height;
        self.maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    } completion:^(BOOL finished) {
    }];
    
}

- (void)dismissComplete:(void (^)(void))complete {
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        self.contentView.tj_y = KScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (complete) {
                complete();
            }
        }];
    }];
}

- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationCustom;
}


@end
